const app = getApp()
import Toast from 'tdesign-miniprogram/toast/index';
Component({

    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
      filterData:[],
      tableData:[],
      queryValue:'',
      cartList:[],
      cartCount:0,
    },

    /**
     * 组件的方法列表
     */
    methods: {
      toBatchPage(e){
        wx.navigateTo({
          url: '/pages/finance/profit/batch/dialog/index',
        })
      },
      getTableData(){
        app.func.request({
          url: '/finance/budget/page',
          data:{},
          success: ({data})=>{
            this.setData({
              tableData: data,
              filterData: data
            })
            this.getCartIdList()
          }
        })
      },
      doAddToCart(e){
        const id = e.currentTarget.dataset.id
        app.func.request({
          url:`/finance/budget/batch/${id}/add`,
          success:({code,msg,data})=>{
              this.getCartIdList()
              Toast({
                context: this,
                selector: '#t-toast',
                message: '加入成功',
                duration:500
              });
          }
        })
      },
      doDelToCart(e){
        const id = e.currentTarget.dataset.id
        app.func.request({
          url:`/finance/budget/batch/${id}/del`,
          success:({code,msg,data})=>{
                this.getCartIdList()
          }
        })
      },
      getCartIdList(){
        app.func.request({
          url:`/finance/budget/batch/list`,
          method: 'GET',
          success:({code,msg,data})=>{
            const cartList =  this.data.tableData.filter(row=>data.indexOf(row.id)>-1)
            const tableData = this.data.tableData
            tableData.forEach(row=>{
              row.dot = cartList.map(s=>s.id).indexOf(row.id)>-1
              return row
            })
            this.setData({
              cartList: cartList,
              cartCount: cartList.length,
              tableData: tableData
            })
            this.queryInput()
          }
        })
      },
      queryInput(e){
        const filterData = this.data.tableData.filter(row=>{
            return row.skuName.indexOf(this.data.queryValue)>-1 || row.skuTitle.indexOf(this.data.queryValue)>-1 
        });
        this.setData({
          filterData: filterData
        });
      },
    },
    pageLifetimes:{
      show: function () {
        this.getTableData()
       },
    },
    ready: function() {
      wx.setNavigationBarTitle({
        title: '多品测算',
      })
    },
})